/**
 * 描述：线段树
 * 把线段树当成满二叉树。
 * @author WangB
 * @create 2018-10-22-22:08
 */
public class SegmentTree<E>{

    private E[] data;
    private E[] tree;//线段树的结构
    private Merger<E> merger;



    public SegmentTree(E[] arr,Merger<E> merger){
        this.merger = merger;//对线段树的两个区间是如何融合的
        data = (E[])new Object[arr.length];
        for ( int i = 0 ; i <arr.length  ; i++ ) {
            data[i] = arr[i];
        }
        tree = (E[])new Object[4*arr.length];
        buildSegmentTree(0,0,data.length - 1);

    }

    /**
     * 再treeIndex的位置创建表示区间[l,r]的线段树
     * @param treeIndex
     * @param l
     * @param r
     */
    private void buildSegmentTree(int treeIndex,int l ,int r){
        if(l == r){
            tree[treeIndex] = data[l];
            return;
        }

        int leftTreeIndex = leftChild (treeIndex);
        int rightTreeIndex = rightChild (treeIndex);

        int mid =l + (r - l) / 2;

        buildSegmentTree (leftTreeIndex,l,mid);
        buildSegmentTree (rightTreeIndex,mid + 1,r);

        tree[treeIndex] = merger.merge (tree[leftTreeIndex],tree[rightTreeIndex]);

    }
    public E get(int index){
        if(index < 0 || index >= data.length)
            throw new IllegalArgumentException ("index wrong");
        return data[index];

    }

    public int getSize(){
        return data.length;
    }

    private int leftChild(int index){
        return index * 2 + 1;
    }

    private int rightChild(int index){
        return index * 2 + 2;
    }

    /**
     * 线段树的查询。
     * 返回区间[queryL,queryR]的值。左右都是闭合的。
     * @param queryL
     * @param queryR
     * @return
     */
    public E query(int queryL,int queryR){
        //边界检查
        if(queryL < 0 || queryL >= data.length ||
                queryR < 0 || queryR >= data.length ||
                  queryL > queryR )
            throw new IllegalArgumentException ("Index wrong");

        return query(0,0,data.length-1,queryL,queryR);
    }

    /**
     * 在以treeIndex为根的线段树[l....r]的范围里。
     * 搜索区间[queryL....queryR]的值
     * @param treeIndex
     * @param l
     * @param r
     * @param queryL
     * @param queryR
     * @return
     */
    private E query(int treeIndex,int l ,int r,int queryL,int queryR){
        if(l ==queryL && r ==queryR)
        {
            return tree[treeIndex];
        }

        int mid = l + (r - l) / 2;
        int leftTreeIndex = leftChild (treeIndex);
        int rightTreeIndex = rightChild (treeIndex);

        if(queryL >= mid + 1)
            return query(rightTreeIndex,mid + 1,r ,queryL,queryR);
        else if(queryR <= mid)
            return query (leftTreeIndex,l,mid,queryL,queryR);

        E leftResult = query(leftTreeIndex,l,mid,queryL,mid);
        E rightResult = query(rightTreeIndex,mid + 1,r ,mid + 1,queryR);
        return merger.merge (leftResult,rightResult);//调用融合器
    }

    /**
     * 将index上的值改成e
     * @param index
     * @param e
     */
    public void set(int index,E e){
        if(index < 0 || index >= data.length)
            throw new IllegalArgumentException ("index wrong");
        data[index] = e;
        set(0 ,0 ,data.length -1,index,e);

    }

    /**
     * 在以treeIndex为根的线段树中更新index的值为e
     * 类比二分搜索树。
     * @param treeIndex
     * @param l
     * @param r
     * @param e
     */
    private void set(int treeIndex,int l ,int r,int index, E e){
        if(l == r){
            tree[treeIndex] = e;
            return;
        }

        int mid = l + (r - l) / 2;
        int leftTreeIndex = leftChild (treeIndex);
        int rightTreeIndex = rightChild (treeIndex);

        if(index >= mid + 1)
            set(rightTreeIndex,mid + 1,r,index,e);
        else
            set(leftTreeIndex,l,mid,index,e);

        tree[index] = merger.merge (tree[leftTreeIndex],tree[rightTreeIndex]);

    }

    @Override
    public String toString(){
        StringBuilder ret = new StringBuilder ();
        ret.append ('[');
        for ( int i = 0 ; i < tree.length ; i++ ) {
            if(tree[i] != null)
                ret.append (tree[i]);
            else
                ret.append ("null");

            if(i != tree.length -1)
                ret.append (',');
        }
        ret.append (']');
        return ret.toString ();
    }
}
